<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use  think\Request;

class Teacher extends Base
{     
    
	
		
	public function Index(){
		
		 return view();
	}
	
	
	
	function Add(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["teachername"] =$request->post('teachername');
				$data["phone"] =$request->post('phone');
				$data["sex"] =$request->post('sex');
				$data["idcard"] =$request->post('idcard');
				$data["overschool"] =$request->post('overschool');
				$data["jointime"] =$request->post('jointime');
				$data["outtime"] =$request->post('outtime');
				$data["otherphone"] =$request->post('otherphone');
			
				
			if($data["id"]){
				
			
				
				if(db('teacher')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				
			 $data["mid"] = session("mid");
		 
				$teacher = db("teacher")->where("phone='".$data["phone"]."'")->find();
				if($teacher){
					return ['status'=>500,'message'=>'手机号不能重复'];
				}else{
					if(db('teacher')->insertGetId($data)){
					
						return ['status'=>200,'message'=>'添加成功'];
					}else{
						return ['status'=>500,'message'=>'修改失败'];
					}
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('teacher')->where("id=".$_GET["id"])->find();
				if(strtotime($info["jointime"])<=0){
					$info["jointime"]="";
				}else{
					$info["jointime"]=date("Y-m-d",strtotime($info["jointime"])); 
				}
	
	
				if(strtotime($info["outtime"])<=0){
					$info["outtime"]="";
				}else{
					$info["outtime"]=date("Y-m-d",strtotime($info["outtime"])); 
				}


				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
	}
	 public function GetTeacher(){
		$where="mid=".session("mid");
		

		$count =db('teacher')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('teacher')->where($where)->order("id desc")->limit($first,$_GET["size"])->select();

		foreach ($list as $key => &$value) {
			$user = db("teacheranduser")->where("teacherid=".$value["id"])->find();
			if($user){
				$value["isbind"]="是";
			}else{
				$value["isbind"]="否";
			}
			
			
			if(strtotime($value["jointime"])<=0){
				$value["jointime"]="";
			}else{
				$value["jointime"]=date("Y-m-d",strtotime($value["jointime"])); 
			}


			if(strtotime($value["outtime"])<=0){
				$value["outtime"]="";
			}else{
				$value["outtime"]=date("Y-m-d",strtotime($value["outtime"])); 
			}


			
			

			
		}

		$obj["count"]=(int)($count/$_GET["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	 public function GetTeacherList(){
		 $request = Request::instance();
		 
		 
		 $where="mid=".session("mid");
		$list = db('teacher')->where($where)->select();
	
		return json($list);
		
	}
	function Del(){
		$request = Request::instance();
		$id =$request->post('id');
		
			$where = 'id='.$id;
		  
		   $list=db('teacher')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	public function importdata(){
		$request = Request::instance();
		$data =$request->post('data');
		$de_json = json_decode($data,TRUE);
		$count_json = count($de_json);
		for ($i = 0; $i < $count_json; $i++)
	    {
				    $s1["teachername"] = $de_json[$i]['teachername'];
					$s1["phone"] = $de_json[$i]['phone'];
					$s1["sex"] = $de_json[$i]['sex'];
					$s1["idcard"] = $de_json[$i]['idcard'];
					$s1["overschool"] = $de_json[$i]['overschool'];
					$s1["jointime"] = $de_json[$i]['jointime'];
					$s1["outtime"] = $de_json[$i]['outtime'];
					$s1["otherphone"] = $de_json[$i]['otherphone'];
					$s1["mid"] = session("mid");

					db('teacher')->insertGetId($s1);
  
		}

		return ['status'=>200,'message'=>'导入成功'];

	}
	function jiebang(){
		$request = Request::instance();
		$id =$request->post('id');

		$where="teacherid=".$id;
		$list=db('teacheranduser')->where($where)->delete();
		if($list!==false) {
			return ['status'=>200,'message'=>'操作成功'];
		  }else{
			 return ['status'=>500,'message'=>'操作失败'];
		  }
	}
	function yuanzhangset(){
		$request = Request::instance();
		$id =$request->post('id');
		$state =$request->post('state');

		$obj = Db::execute('update teacher set isyuanzhang=? where id=?',[$state,$id]);
		if($list!==false) {
			return ['status'=>200,'message'=>'操作成功'];
		  }else{
			 return ['status'=>500,'message'=>'操作失败'];
		  }
	}
	function myclass(){
		$request = Request::instance();
		 
		$this->assign('id',$request->get("id"));
				
		return view();
	}
	function myclasslist(){
		$request = Request::instance();
		$id =$request->post('id');
		$where="teacherid=".$id;
		$list = db('teacherandclass')->where($where)->select();
		foreach ($list as $key => &$value) {
			$myclass = db("myclass")->where("id=".$value["classid"])->find();
			$value["classname"]=$myclass["classname"];
		}
		return json($list);



		
	}
	function guanliandel(){
		$request = Request::instance();
		$id =$request->post('id');

		$where="id=".$id;
		$list=db('teacherandclass')->where($where)->delete();
		if($list!==false) {
			return ['status'=>200,'message'=>'操作成功'];
		  }else{
			 return ['status'=>500,'message'=>'操作失败'];
		  }
	}
	function teacherandclassadd(){
		$request = Request::instance();
		$id =$request->post('id');
		$classid =$request->post('classid');
		$data["teacherid"]=$id;
		$data["classid"]=$classid;
		$data["mid"]=session("mid");
		$data["createtime"]=date('Y-m-d H:i:s',time());


		$info = db("teacherandclass")->where("classid=".$classid." and teacherid=".$id)->find();
		if($info){
			return ['status'=>500,'message'=>'不能重复绑定'];
		}else{
			if(db("teacherandclass")->insertGetId($data)){
				return ['status'=>200,'message'=>'操作成功'];
			}else{
				return ['status'=>500,'message'=>'操作失败'];
			}

		}

	

		
	}
}
